3 Functions
So far, you have seen built-in functions of Python such as print()
, len()
& input()
functions. Functions are like mini-programs that are meant to do a single task like the print()
function prints the string on your terminal. Let’s break it down in simple terms to an 8th-grader child:
Imagine you have a box, and you can put different tasks or instructions inside that box. In programming, we call this box a “function.”
Now, why do we use functions? Well, think of it like having a set of instructions that you might need to use multiple times. Instead of writing those instructions again and again, you put them in a box (function). So, whenever you need to follow those instructions, you just use the box (call the function) instead of writing everything out each time.
# Define the function
1def add_numbers():
= 12 + 12
result 2return result
# Print the result
3print("The sum is:", add_numbers())
- 1
-
def
means define and is used to define a function in Python. - 2
-
return
In programming, the"return"
keyword is like telling a function to give you something back. It’s a way for the function to share a specific result or piece of information with the rest of the program. Just as you put something in a box and then open the box to get it later,"return"
allows a function to hand back its result when it’s done. - 3
-
In this case
add_numbers()
function will return 24 and place it in the print function.
3.1 Functions with Arguments
When you pass the string to the print()
function between parenthesis is called the arguments. Think of an argument as an extra set of powers a function can have. Let’s look at the following analogy: Imagine you have a Robot friend and you want to give it 2 numbers to add them and give you the result. Every time you give it different numbers and will give you the sum of it.
# Copy and Paste it into your shell window to see its output:
1def robot(num1 , num2):
print(num1 + num2)
223, 42)
robot(3, 2)
robot(33, 12) robot(
- 1
-
num1
&num2
are parameters (which is an empty Box) - 2
-
23
&42
are the arguments that will go inside thenum1
&num2
parameters.
3.2 The None Value
In Python, we have a value called None
which represents nothing or the absence of a value. The None
keyword must be started with capital ‘N’. None
is the only data type of the NoneType
class. (will explain later what are classes)
def func_none(response):
if response == 'Gold' or response == 'Silver':
print('You Got Gold!!')
elif response == 'Gold' or response == 'Silver':
print('You Got Gold!!')
else:
return func_none
1= func_none(response=input('Choose Gold or Silver, If you choose anything else you will get "None" '))
result
2print(result)
- 1
-
Here, the program will ask the user to enter the value and that value will be passed to the
func_none()
function as an argument and there the condition would be applied to the corresponding value if none of the conditions met, then the function returns theNone
value. - 2
-
Here, if
func_none()
returns the result of the condition met like “Gold” or “Silver” then in the second line it would also printNone
and that’s becauseprint()
function on itself return nothing which is basicallyNone
.
3.3 Local and Global Scope
Let’s look at the code first and then we will see what is Local and Global.
1= 'asad'
name
def func1():
= 'wafi'
name 2print(name)
def func2():
= 18
age 3print('Name: ',name)
print('Age: ',age)
func1() func2()
- 1
-
Here, the
name
variable can be accessed everywhere in the same program file e.g.func1.py
while thename
variable inside thefunc1()
function can only be accessed inside that function. - 2
- Gives you wafi` in output.
- 3
-
Here the
name
variable will return the value ‘asad’ from the Global scope.
3.4 Global Statement
If you want to modify the Global variable inside a function you can use the global
keyword before that variable but do not create a variable with the same name as the Global variable inside that function.
# Global variable
= 5
counter
def increase_counter():
global counter
+= 1
counter
# Before calling the function
print("Before: Counter =", counter)
# Calling the function to modify the global variable
increase_counter()
# After calling the function
print("After: Counter =", counter)
If you ever wanted to change the value of the Global variable you must use the global statement on the variable. So that Python knows which variable you are referring to.
3.5 Exception Handling
Getting an error or exception, in your Python programs means that the entire program will crash, But we don’t want this to happen in real-world scenarios, rather than you want the program to detect the error handle them and continue afterward.
For instance, run the following code in the editor:
def division(divider):
print(42 / divider)
2) # Passing argument 2 to the function.
division(7)
division(10)
division(1) division(
- 1
-
Here when you pass 0 dividers to the
division()
function means you are dividing 42 by 0 which will give you an error like the one below:
print(42 / divider)
1ZeroDivisionError: division by zero
- 1
-
ZeroDivisionError
is an exception raised by an error of dividing the number by zero.
Now to prevent such types of exceptions & errors, Python has try
and except
clauses. The code that causes the error will go inside the try
clause and the code that prevents the program from crashing goes into the except clause.
For example, Look at the above code with try
and except
clause:
def division(divider):
try:
print(42 / divider)
1except ZeroDivisionError:
print('You cannot divide with Zero')
20) division(
- 1
-
ZeroDivisionError
tells Python if you encounter ZeroDivisionError raise the following print message But you can write anything below except clause. - 2
- Now when you call the division function with 0 number it won’t crash the entire program but will give you a decent error message and the program will continue to execute other remaining instructions.
3.6 ZigZag Program for Fun:
Let’s use your previous knowledge of programming to create a small animation zigzag program. Type the following code into your file editor & save the file as zigzag.py:
2import time
import sys
try:
1while True:
= 4
counter
for i in range(10):
if i <= 4:
print(f'{counter*" "}********')
-= 1
counter elif counter == -1 or counter == 0:
= 1
counter else:
print(f'{" "*counter}********')
+= 1
counter
30.3) # Adjust the delay time as needed to make it more like animation
time.sleep(except KeyboardInterrupt:
sys.exit()
- 1
-
The
while
loop will never exit until you press Ctrl+cCtrl+c which calls theKeyboardInterrupt
Error. - 2
-
In Python, the
time
module helps you work with time-related functions. It’s like a toolbox with tools to handle time-related tasks. - 3
-
Try to write a program without
try
andexcept
which will cause the program to crash if you want to stop the while loop by Ctrl+cCtrl+c and will give you an ugly error message. However, for our program we it to cleanly handleKeyboardInterrupt
in except clause then pressing Ctrl+cCtrl+c won’t crash the program rather it will stop smoothly as usual by calling thesys.exit()
function.
Functions are the primary way to prevent you from duplicating your code. Functions are great to help you organize your code. You can think of it as the black boxes: they have input in the form of parameters
and have output in the form of return
values.